﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Core.Authenticated;
using Core.Library;
using DataObject;
using DataObject.Category;

namespace CMS.Controls.Category
{
    public partial class CreateOrEdit : AuthenticatedControl
    {
        protected int CategoryId;
        protected int TypeId;

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId  = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
            TypeId      = Utils.GetIdFromRequestString(Utils.TypeId, 0);

            ltTitle.Text = (int)CategoryType.Product == TypeId
                                ? "Tạo mới danh mục sản phẩm" 
                                : "Tạo mới danh mục tin tức";

            if (!IsPostBack)
            {
                ListCategory();

                if (CategoryId > 0)
                {
                    ltTitle.Text = (int)CategoryType.Product == TypeId
                                ? "Sửa danh mục sản phẩm"
                                : "Sửa danh mục tin tức";
                    BindCategory(CategoryId);
                }
                    
            }
        }

        private void ListCategory()
        {
            var categories = new List<CategoryObject>();
            CategoryController.GetListTree(categories, TypeId, 0, 0);
            if (categories.Count > 0)
            {
                ddlParent.DataSource = categories;
                ddlParent.DataBind();                
            }
            ddlParent.Items.Insert(0, new ListItem("-- Gốc --", "0"));
        }
        
        private void BindCategory(int id)
        {
            var o = CategoryController.Category_GetById(id);

            if (o != null)
            {
                txtName.Text        = o.Name;   
                txtPriority.Text    = o.Priority.ToString();
                txtImage.Text       = o.ImagePath;
                cbIsActive.Checked  = (bool)o.IsActive;
            }

            try { ddlParent.SelectedValue = o.ParentId.ToString(); }
            catch { ddlParent.SelectedValue = "0"; }
        }

        protected void Save(object sender, EventArgs e)
        {
            CategoryObject o;

            var id = Convert.ToInt32(CategoryId);
            var parentId = ddlParent.SelectedValue.Equals("0")
                                ? 0
                                : Convert.ToInt32(ddlParent.SelectedValue);

            if (id > 0)
            {
                o = CategoryController.Category_GetById(id);
                o.CategoryId    = id;                
                o.ParentId      = parentId;
                o.Priority      = ConvertUtility.ToInt32(txtPriority.Text);
                o.TypeId        = TypeId;
                o.Name          = txtName.Text;
                o.Url           = UnicodeUtility.UnicodeToKoDauAndGach(txtName.Text);
                o.ImagePath     = txtImage.Text;
                o.IsActive      = cbIsActive.Checked;
                CategoryController.Category_Update(o);
            }
            else
            {
                o = new CategoryObject();
                o.ParentId      = parentId;
                o.Priority      = ConvertUtility.ToInt32(txtPriority.Text);
                o.TypeId        = TypeId;
                o.Name          = txtName.Text;
                o.Url           = UnicodeUtility.UnicodeToKoDauAndGach(txtName.Text);
                o.ImagePath     = txtImage.Text;
                o.IsActive      = cbIsActive.Checked;
                CategoryController.Category_Create(o);
            }

            Response.Redirect((int)CategoryType.Product == TypeId
                                  ? UrlHelpers.ListCategoryProduct
                                  : UrlHelpers.ListCategoryArticle);
        }
    }
}